package com.os.crm.controller;

import com.os.crm.pojo.FileVersion;
import com.os.crm.service.DataTaskService;
import com.os.crm.service.POIService;
import com.os.util.BPConfiguration;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@RestController
@CrossOrigin
@RequestMapping("/file")
@RefreshScope
@Slf4j
public class FileController {
    @Autowired
    private DataTaskService dataTaskService;
    @Autowired
    private POIService poiService;

    @ApiOperation(value = "查询服务端版本号" , notes = "查询服务端版本号")
    @RequestMapping(value="/getLatestVersion", method=RequestMethod.GET)
    public Result getLatestVersion(){
        FileVersion fileVersion = new FileVersion();
        fileVersion.setVersionCode(1);
        return new Result(true, StatusCode.OK,"查询成功", fileVersion);
    }
    /**
     * 下载APK安装包
     * 模版位置: files\template
     * @param request
     * @param response
     */
    @RequestMapping(value="/getApk", method=RequestMethod.GET)
    public void doGetApk(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String sheet_type = "";//lend.getLendtype_key() ;
        String file_name = "app-release-1.0.apk";
        String file_path = BPConfiguration.getPathBase()+ "/apk/"+file_name;
        File file = new File(file_path);
        response.setContentType("application/octet-stream");
        //fileName = new String(fileName.getBytes("gb2312"),"ISO8859_1");
        //file_name = new String(file_name.getBytes("gb2312"),"utf-8");
        file_name = new String(file_name.getBytes("gbk"),"ISO8859_1");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + file_name + "" + "\"");
        int len = (int)file.length();
        byte []buf = new byte[len];
        FileInputStream fis = new FileInputStream(file);
        OutputStream out = response.getOutputStream();
        len = fis.read(buf);
        out.write(buf, 0, len);
        out.flush();
        fis.close();
    }
}